import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
// @ts-ignore
import vueJsx from "@vitejs/plugin-vue-jsx";

//element plus自动按需导入组件
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
const path = require("path");
import ElementPlus from "unplugin-element-plus/vite";

// https://vitejs.dev/config/
export default defineConfig(
  (envData) => {
    // console.log('[ mode ]', envData)
    // console.log('[ mode.mode ]', envData.mode)
    // console.log('[ process.env.NODE_ENV ]', process.env.NODE_ENV)
    return {
      base: "./",
      css: {
        preprocessorOptions: {
          scss: {
            additionalData: `@use "@/style/element/index.scss" as *;`,
          },
        },
        postcss: {
          plugins: [
            {
              postcssPlugin: "internal:charset-removal",
              AtRule: {
                charset: (atRule) => {
                  if (atRule.name === "charset") {
                    atRule.remove();
                  }
                },
              },
            },
          ],
        },
      },
      resolve: {
        // alias: {
        //   "@": path.resolve(__dirname, "src"),
        // },
        alias: [
          {
            find: "vue",
            replacement: "vue/dist/vue.esm-bundler.js",
          },
          {
            find: "@",
            replacement: path.resolve(__dirname, "src"),
          },
          {
            find: "@antv/x6",
            replacement: "@antv/x6/dist/x6.js",
          },
          {
            find: "@antv/x6-vue-shape",
            replacement: "@antv/x6-vue-shape/lib",
          },
        ],
      },
      define: {
        "process.env": process.env,
      },
      plugins: [
        vue(),
        ElementPlus({
          useSource: true,
        }),
        AutoImport({
          imports: ["vue", "vuex", "vue-router"],
          dts: "typings/auto-import.d.ts",
          resolvers: [
            ElementPlusResolver({
              importStyle: "sass",
            }),
          ],
          eslintrc: {
            enabled: true
          },
        }),
        Components({
          dts: "typings/components.d.ts",
          resolvers: [
            ElementPlusResolver({
              importStyle: "sass",
            }),
          ],
        }),
        vueJsx({
          // options are passed on to @vue/babel-plugin-jsx
        }),
      ],
      build: {
        target: 'es2020',
        cssTarget: ['chrome86', 'safari14', 'firefox65'],
        // minify: false,
        // sourcemap: true,
        sourcemap: false,
        minify: "terser",
        terserOptions: {
          compress: {
            // 生产环境时移除console
            // drop_console: process.env.NODE_ENV === "production",
            drop_console: false,
            drop_debugger: true,
          },
        },
      }
    }
  }
);
